import { ref } from 'vue';

export function useCounter(initialCount = 0, countDownTime = 60) {
  const count = ref(initialCount);
  let timer: NodeJS.Timeout | null = null;

  const handleCounter = () => {
    if (count.value > 0) return;

    count.value = countDownTime;
    timer = setInterval(() => {
      count.value--;
      if (count.value <= 0) {
        clearInterval(timer!);
        timer = null;
      }
    }, 1000);
  };

  const resetCounter = () => {
    if (timer) {
      clearInterval(timer);
      timer = null;
    }
    count.value = initialCount;
  };

  return [count, handleCounter, resetCounter] as const;
}
